package com.factory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Query;
import org.hibernate.Session;

import com.bean.Login;
import com.bean.User;
import com.common.HibernateSessionFactory;

public class LoginInfor {
	public static List loginValidate(String username, String password) {
		Session session = HibernateSessionFactory.getSessionFactory()
				.openSession();
		String hql = "select u from Login as u where u.loginName=?and u.loginPassword=? ";
		Query query = session.createQuery(hql);
		query.setString(0, username);
		query.setString(1, password);
		List<Login> next = query.list();
		List userList = new ArrayList();
		try {
			for (int i = 0; i < next.size(); i++) {
				Map<String, String> ingredients = new HashMap<String, String>();
				ingredients.put("userId", next.get(i).getUser().getUserId());
				ingredients
						.put("userName", next.get(i).getUser().getUserName());
				ingredients.put("loginName", next.get(i).getLoginName());
				ingredients
						.put("loginPassword", next.get(i).getLoginPassword());
				ingredients.put("userType", next.get(i).getUser()
						.getReadertype().getReaderType());
				userList.add(ingredients);
			}
			return userList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
}
